// pages/activity/tree/index.js
Page({
  data: {
    signDays: 0,
    treeLevel: "",
    rankList: [],
    dialog_calendar: false,
    date_calendar: "",
    signStatus_calendar: [],
    reSignDate: -1,
    id: ""
  },
  onLoad(options) {
    // 今日是否已签到
    getApp().globalData.http.tree_signStatus({
      startDate: this.getToday(),
      endDate: this.getToday()
    }).then(res => {

    });
    this.getToday()
    this.getSignDays();
    this.getSignStatus(this.getFirstDay(), this.getLastDay());
    this.getRank();
  },
  onShow() {},
  onShareAppMessage() {},
  sign() {
    getApp().globalData.http.tree_sign({
      type: 0
    }).then(res => {
      getApp().globalData.vant.toast.success("签到成功");
      this.onLoad();
    }).catch(e => {
      getApp().globalData.vant.toast.fail(e.msg);
    });
  },
  getSignDays() {
    getApp().globalData.http.tree_signDays().then(res => {
      this.setData({
        signDays: res.data
      });
      let days = res.data % 180;
      if (days < 31) {
        this.setData({
          treeLevel: 1
        });
      } else if (days < 61) {
        this.setData({
          treeLevel: 2
        });
      } else if (days < 91) {
        this.setData({
          treeLevel: 3
        });
      } else if (days < 121) {
        this.setData({
          treeLevel: 4
        });
      } else if (days < 151) {
        this.setData({
          treeLevel: 5
        });
      } else if (days < 181) {
        this.setData({
          treeLevel: 6
        });
      }
    });
  },
  getSignStatus(startDate, endDate) {
    getApp().globalData.http.tree_signStatus({
      startDate: startDate,
      endDate: endDate
    }).then(res => {
      let date = res.data[0].date.split("-");
      this.setData({
        date_calendar: date[0] + "年" + date[1] + "月",
      });
      this.drawCalendar(res.data.reverse());
    });
  },
  drawCalendar(data) {
    let days = data;
    days.unshift({
      status: 0,
      date: "2022-07-01",
      isResin: 0
    });
    let start = new Date(days[0].date).getDay();
    let end = new Date(days[days.length - 1].date).getDay();
    for (let i = start - 1; i > 0; i--) {
      days.unshift({
        status: 0,
        date: null,
        isResin: 0
      });
    }
    if (end != 0) {
      for (let i = 0; i < 7 - end; i++) {
        days.push({
          status: 0,
          date: null,
          isResin: 0
        });
      }
    }
    this.setData({
      signStatus_calendar: days
    });
  },
  judgeMonthDays(date) {
    let year = date.split("-")[0] / 1;
    let month = date.split("-")[1] / 1;
    if (month == 2) {
      if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        return 29;
      } else {
        return 28;
      }
    } else if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
      return 31;
    } else {
      return 30;
    }
  },
  getRank() {
    getApp().globalData.http.tree_rank({
      page: 1,
      size: 10
    }).then(res => {
      this.setData({
        rankList: res.data
      });
    });
  },
  navTo(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.path,
    });
  },
  getFirstDay() { //当前月第一天
    var y = new Date().getFullYear();
    var m = new Date().getMonth() + 1;
    var d = '01';
    m = m < 10 ? '0' + m : m;
    return [y, m, d].join("-")
  },
  getLastDay() { //当前月最后一天
    var y = new Date().getFullYear();
    var m = new Date().getMonth() + 1;
    var d = new Date(y, m, 0).getDate();
    m = m < 10 ? '0' + m : m;
    return [y, m, d].join("-")
  },
  getToday() { //今天
    var y = new Date().getFullYear();
    var m = new Date().getMonth() + 1;
    var d = new Date().getDate();
    m = m < 10 ? '0' + m : m;
    return [y, m, d].join("-")
  },
  getDateFromString(str) {
    var reg = /^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)/;
    var s = str.match(reg);
    var result = "";
    if (s) {
      result = new Date(s[1], s[2] - 1, s[3], s[4], s[5], s[6]).getTime();
    }
    return result;
  },
  calendarClose() {
    this.setData({
      dialog_calendar: false
    });
  },
  reSignCard() {
    this.setData({
      dialog_calendar: true
    });
  },
  selectReSignDate(e) {
    this.setData({
      reSignDate: e.currentTarget.dataset.date
    });
  },
  supSignConfirm() {
    if (this.data.reSignDate == -1) {
      getApp().globalData.vant.toast("请选择补签日期");
    } else {
      getApp().globalData.http.tree_sign({
        type: 1,
        createTime: this.data.reSignDate
      }).then(res => {
        getApp().globalData.vant.toast.success("补签成功");
        this.setData({
          dialog_calendar: false
        });
      }).catch(e => {
        getApp().globalData.vant.toast.fail(e.msg);
      });
    }
  },
  scrollTo(e) {
    console.log(e.currentTarget.dataset.id)
    let query = wx.createSelectorQuery();
    query.select("#" + e.currentTarget.dataset.id).boundingClientRect((res) => {
      wx.pageScrollTo({
        scrollTop: res.top,
        duration: 1000,
      })
    }).exec();
  }
})